/**
 *    Copyright 2006-2017 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.plugins.custome;

import java.util.Collections;
import java.util.List;

/**
 * 
 * @Type PageQuery
 * @Desc 查询分页基类
 * @author shuai.zuo
 * @date 2012-10-18
 * @Version V1.0
 */
public class PageQuery<T> {

    /**
     * 执行所有查询语句后返回的结果集,与DAO中的listByQuery的返回值对应
     */
    protected List<T> dataList;

    /** 默认每页显示的记录数 */
    public static final int DEFAULT_PAGE_SIZE = 20;

    /** 每页最多显示的记录数 */
    public static final int MAX_PAGE_SIZE = 200;

    /**
     *
     * 一页大小
     */
    protected int pageSize;
    /**
     * 总记录数
     */
    protected int totalRecord;
    /**
     * 当前页数，从 1开始，1代表第一页
     */
    protected int pageIndex;
    /**
     * 总页数
     */
    protected int totalPage;

    /** 当前页下首条数据在数据库中的index */
    protected int recordIndex;
    
    /**
     * 数据库分页中，开始于第几条
     */
    protected int start;
    /**
     * 数据库分页中，结束于第几条
     */
    protected int end;
    
    

    public int getStart() {
        return getStartPos();
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return getEndPos();
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public int getRecordIndex() {
        return getStartPos();
    }

    /**
     * 获取一页的记录数
     * 
     * @return
     */
    public int getPageSize() {
        if (pageSize < 1) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        return pageSize;
    }

    /**
     * 设置一页的记录数
     * 
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 总记录数
     * 
     * @return
     */
    public int getTotalRecord() {
        return totalRecord;
    }

    /**
     * 设置总的记录数。计算出总页数
     * 
     * @param totalRecord
     */
    public void setTotalRecord(int totalRecord) {
        this.totalPage = (totalRecord + getPageSize() - 1) / getPageSize();
        this.totalRecord = totalRecord;
    }

    /**
     * 得到当前查询的第几页
     * 
     * @return
     */
    public int getPageIndex() {
        if (pageIndex > getTotalPage()) {
            pageIndex = getTotalPage();
        }
        if (pageIndex < 1) {
            pageIndex = 1;
        }
        return pageIndex;
    }

    /**
     * 是否有下页
     * 
     * @return
     */
    public boolean hasNextPage() {
        if (pageIndex < getTotalPage()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置当前页面
     * 
     * @param pageIndex
     */
    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getStartPos() {
        return (getPageIndex() - 1) * getPageSize();
    }

    public int getEndPos() {
        if (getPageIndex() * getPageSize() < getTotalRecord()) {
            return getPageIndex() * getPageSize();
        } else {
            return getTotalRecord();
        }
    }

    /**
     * 得到总页数
     * 
     * @return
     */
    public int getTotalPage() {
        return totalPage;
    }

    public List<T> getDataList() {
        if (dataList == null) {
            return Collections.<T> emptyList();
        } else {
            return dataList;
        }
    }

    public void setDataList(List<T> module) {
        this.dataList = module;
    }

    public int getPage() {
        return this.pageIndex;
    }

    /**
     * 同pageIndex
     * 
     * @param page
     */
    public void setPage(int page) {
        setPageIndex(page);
    }

}
